//
//  GQHContractDetailController.m
//  Xanthium
//
//  Created by GuanQinghao on 09/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model
#import "GQHContractModel.h"

#pragma mark Object

#pragma mark View
#import "GQHContractDetailView.h"

#pragma mark Controller
#import "GQHContractDetailController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHContractDetailController () {
    
#pragma mark Data
    /// 请求参数
    NSMutableDictionary *parameters;
    /// 返回模型数组
    __block GQHContractModel *contractModel;
#pragma mark View
    /// 自定义根视图
    GQHContractDetailView *rootView;
}

@end


@implementation GQHContractDetailController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHContractDetailView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:_qh_contractID forKey:@"id"];
    [self fetchServiceDataWithParameters:parameters];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"承运协议";
    
    if (_qh_acceptable) {
        
        self.qh_rightButton.hidden = YES;
    } else {
        
        [self.qh_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    }
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    DLog(@"同意协议内容");
    
    NSMutableDictionary *para = [NSMutableDictionary dictionary];
    [para setObject:contractModel.qh_contractID forKey:@"waybillAgId"];
    [para setObject:contractModel.qh_freightID forKey:@"goodsSupplyId"];
    
    NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buWaybill/1/sure"];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [GQHNetHelper qh_requestGET:APIURLString parameters:para success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (0 == [code integerValue]) {
            
            [self showToastWithText:message];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --PrivateMethod
- (void)fetchServiceDataWithParameters:(NSMutableDictionary *)parameters {
    
    [GQHContractModel qh_fetchContractWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, GQHContractModel *contract) {
        
        if (status) {
            
            contractModel = contract;
            rootView.qh_contractModel = contractModel;
            [rootView.qh_tableView reloadData];
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
